#include "../head/server.h"

// 处理gets命令，即发送文件
int gets_work(int net_fd, char *user_name, MYSQL *mysql)
{
	int netfd = net_fd;
	train_t train;

	// 接收客户端传来的路径path，需要手动截取文件名
	char path[1024] = {0};
	int path_len;
	recv(netfd, &path_len, sizeof(int), MSG_WAITALL);
	recv(netfd, path, path_len, MSG_WAITALL);

	// 调用函数检查客户端发来的路径（文件）path是否存在
	int existance_ret = check_file_existance(mysql, path, user_name); // TODO...
	if (existance_ret)
	{ // 返回0就是存在，返回1就是不存在
		// 不存在
		printf("no\n");
		bzero(&train, sizeof(train));
		train.length = strlen("no");
		memcpy(train.data, "no", train.length);
		send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
		return 1; // 返回退出函数
	}
	else
	{
		printf("yes\n");
		// 存在，通知客户端接收文件
		bzero(&train, sizeof(train));
		train.length = strlen("yes");
		memcpy(train.data, "yes", train.length);
		send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
	}

	// 截取文件名
	char file_name[1024];
	int i;
	for (i = path_len - 1; i > 0; i--)
	{
		if (path[i] == '/')
		{
			// 路径从后往前的第一个'/'
			break;
		}
	}
	if (i)
	{
		// 把从后往前第一个'/'后面的内容赋值给file_name
		strcpy(file_name, (path + i + 1));
	}
	else
	{
		// i==0,意味path存的就是个文件名
		strcpy(file_name, path);
	}

	printf("要打开的文件名为：%s\n", file_name);

	// 拼接配置文件的root_path
	char full_path[1024] = {0};
	getparameter("root_path", full_path);
	strcat(full_path, "/");
	strcat(full_path, file_name);
	printf("要打开的文件路径为：%s\n", full_path);

	// if('/' == path[0]){
	//     //这是个绝对路径，拼接逻辑路径
	//     strcpy(file_path, client_info->virtual_global_path);
	//     strcat(file_path, path);
	// }else{
	//     //这是个相对路径，拼接真实路径
	//     strcpy(file_path, client_info->global_path);
	//     strcat(file_path, path);
	// }
	// printf("绝对路径为%s\n", file_path);

	bzero(&train, sizeof(train));
	int file_fd;
	// 接受客户端传来的关于这个文件是否已经下载过的信息
	// 如果下载过，那文件的大小是多少
	off_t seek_size = 0;
	recv(netfd, &seek_size, sizeof(off_t), MSG_WAITALL);
	printf("seek_size: %ld\n", seek_size);
	if (seek_size != 0)
	{
		// 客户端下载过
		file_fd = open(full_path, O_RDWR);
		ERROR_CHECK(file_fd, -1, "open");
		lseek(file_fd, seek_size, SEEK_SET);
	}
	else
	{
		// 客户端没下载过
		file_fd = open(full_path, O_RDWR);
		ERROR_CHECK(file_fd, -1, "open");
	}

	// 获得剩余文件的大小，发送给客户端
	struct stat statbuf;
	fstat(file_fd, &statbuf);
	off_t tran_size = statbuf.st_size - seek_size;
	printf("tran_size: %ld\n", tran_size);
	send(netfd, &tran_size, sizeof(off_t), MSG_NOSIGNAL);

	if (tran_size >= BIG_FILE)
	{
		// 大文件
		sendfile(netfd, file_fd, &seek_size, tran_size);
	}
	else
	{
		// 小文件
		while (1)
		{
			ssize_t read_ret = read(file_fd, train.data, sizeof(train.data));
			if (read_ret == 0)
			{
				break;
			}
			// 断点重传测试用
			//  printf("已传输：%ld\n", read_ret);
			// sleep(5);
			train.length = read_ret;
			send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
		}
		// 告诉客户端发送完毕
		train.length = 0;
		send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
	}

	printf("文件传输完成！\n");
	// 关闭文件
	close(file_fd);

	return 0;
}